//
//  LancooEyeCareModeManager.m
//  LancooEyeCarePro
//
//  Created by dangwc on 2019/10/22.
//  Copyright © 2019 dangwc. All rights reserved.
//

#import "LancooEyeCareModeManager.h"

static NSString *const iEyeCareModeStatus = @"iEyeCareModeStatus";
static LancooEyeCareModeManager *eyeCareModel = nil;
#define EC_HEIGHT   [UIScreen mainScreen].bounds.size.height
#define EC_WIDTH   [UIScreen mainScreen].bounds.size.width

@implementation LancooEyeCareModeManager
+(instancetype)shareEyeCare{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        eyeCareModel = [[LancooEyeCareModeManager alloc] init];
    });
    return eyeCareModel;
}


-(BOOL)eyeCareModeStatus{
    return [[NSUserDefaults standardUserDefaults] boolForKey:iEyeCareModeStatus];
}



-(void)switchEyeCareMode:(BOOL)isOpen{
    [[NSUserDefaults standardUserDefaults] setBool:isOpen forKey:iEyeCareModeStatus];
    [[NSUserDefaults standardUserDefaults] synchronize];
    self.skinCoverWindow.hidden = YES;
    __weak typeof(self) weakSelf = self;
    if (isOpen) {
        self.skinCoverWindow.hidden = NO;
        self.previousKeyWindow = [UIApplication sharedApplication].keyWindow;
        [self.skinCoverWindow makeKeyWindow];
        [self.skinCoverWindow openAnimationFinished:^{
            [weakSelf.previousKeyWindow makeKeyWindow];
        }];
    }else{
        [self.skinCoverWindow offAnimationFinished:^{
            if ([[UIApplication sharedApplication].windows containsObject:self.skinCoverWindow]) {
                weakSelf.skinCoverWindow.hidden = YES;
                weakSelf.previousKeyWindow = nil;
            }
        }];
    }
}



-(LancooEyeCareCoverWindow *)skinCoverWindow{
    if (!_skinCoverWindow) {
        _skinCoverWindow = [[LancooEyeCareCoverWindow alloc] initWithFrame:CGRectMake(0, 0, EC_WIDTH, EC_HEIGHT)];
        _skinCoverWindow.windowLevel = 2099;
        _skinCoverWindow.userInteractionEnabled = NO;
    }
    return _skinCoverWindow;
}



@end
